package com.xyht.sca_s.student_manage_system.modules.repairCheck.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.repairCheck.entity.SmsRepairCheckApplyNote;
import com.xyht.sca_s.student_manage_system.modules.repairCheck.entity.vo.SmsRepairCheckApplyNoteVO;
import com.xyht.sca_s.student_manage_system.modules.repairCheck.mapper.SmsRepairCheckApplyNoteMapper;
import com.xyht.sca_s.student_manage_system.modules.repairCheck.service.SmsRepairCheckApplyNoteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-06-25
 */
@Service
public class SmsRepairCheckApplyNoteServiceImpl extends ServiceImpl<SmsRepairCheckApplyNoteMapper, SmsRepairCheckApplyNote> implements SmsRepairCheckApplyNoteService {
    @Autowired
    SmsRepairCheckApplyNoteMapper smsRepairCheckApplyNoteMapper;
    @Override
    public ResponseResult addNote(SmsRepairCheckApplyNoteVO noteVO) {
        if(isNullOrEmpty(noteVO.getApplyNote())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsRepairCheckApplyNote note = new SmsRepairCheckApplyNote();
        note.setApplyNote(noteVO.getApplyNote());
        smsRepairCheckApplyNoteMapper.insert(note);
        return CommonResult.success();
    }

    @Override
    public ResponseResult select() {
        SmsRepairCheckApplyNote one = this.getOne(new LambdaQueryWrapper<SmsRepairCheckApplyNote>()
                .orderByDesc(SmsRepairCheckApplyNote::getUpdateTime)
                .last("limit 1")
        );
        SmsRepairCheckApplyNoteVO noteVO = BeanUtil.copyProperties(one, SmsRepairCheckApplyNoteVO.class);
        return CommonResult.success(noteVO);
    }

    @Override
    public ResponseResult updateNote(SmsRepairCheckApplyNoteVO noteVO) {
        if(isNullOrEmpty(noteVO)){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsRepairCheckApplyNote query = smsRepairCheckApplyNoteMapper.selectById(noteVO);
        if(isNullOrEmpty(query)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        SmsRepairCheckApplyNote note = new SmsRepairCheckApplyNote();
        BeanUtils.copyProperties(noteVO,note);
        smsRepairCheckApplyNoteMapper.updateById(note);
        return CommonResult.success();
    }
}
